package com.compdev.testutils.counterstring;

public class CounterStringGenerator {

	private String delimiter;

	public CounterStringGenerator(String delimiter) {
		this.delimiter = delimiter;
	}

	public String generateCounterString(int numberOfCharsInString) {

		StringBuilder sb = new StringBuilder();

	    Boolean keepGenerating = true;
	    
	    int currPosToGenerate = numberOfCharsInString;
	    
	    do {
	    	String currentPosition = Integer.toString(currPosToGenerate);
	    	
	    	// add the last bit of the string if too big to add
	        if((sb.length() + currentPosition.length() + 1) > numberOfCharsInString){
	        	String delimString = "";
	        	for (int i = 0; i < numberOfCharsInString - sb.length(); i++) {
					delimString = delimString + delimiter;
				}
            	//pad out the string with seperators
            	sb.append(delimString);
            	keepGenerating = false;
	        }
	        
	        if(keepGenerating){
	            sb.append(delimiter);
	            sb.append(new StringBuffer(currentPosition).reverse().toString());
	            currPosToGenerate = currPosToGenerate - currentPosition.length() - 1;
	        }
        
		} while (keepGenerating);
	    
		return sb.reverse().toString();
	}

}
